﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMIS_ERP.Web.Order_Info
{
    public partial class Report_Agent_Factory_OrderArea : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Pub_Useful.CheckRight.checkRight("ag_0|ag_1");
                Bind_GV("2010-1-1", "2110-1-1");
            }
            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string time1 = "2010-1-1", time2 = "2110-1-1";
            if (txthTime1.Text != "")
            {
                time1 = txthTime1.Text.Trim() + " 00:00:00";
            }
            if (txthTime2.Text != "")
            {
                time2 = txthTime2.Text.Trim() + " 23:59:59";
            }
            Bind_GV(time1, time2);
        }

        /// <summary>为GridView绑定数据
        /// 	<remark>2011-03-23 BY 河北大学信管团队</remark>
        /// </summary>
        private void Bind_GV(string strTime1, string strTime2)
        {
            IMIS_ERP.BLL.Factory_Info bll = new BLL.Factory_Info();
            SqlDataReader dr = bll.GetfiID_M(Session["aiProvince"].ToString(), "in(" + IMIS_ERP.BLL.Assistant.StrToQuotation(Session["aiArea"].ToString()) + ")");
            IMIS_ERP.BLL.Order_Info bll1 = new IMIS_ERP.BLL.Order_Info();
            DataSet ds = bll1.GetFiSumAll_Area(Session["aiProvince"].ToString(), "in(" + IMIS_ERP.BLL.Assistant.StrToQuotation(Session["aiArea"].ToString()) + ")", strTime1, strTime2);
            DataView dv1 = ds.Tables[0].DefaultView, dv2 = ds.Tables[1].DefaultView, dv3 = ds.Tables[2].DefaultView, dv4 = ds.Tables[3].DefaultView;
            DataTable dt = new DataTable();
            dt.Columns.Add("oiFiID");
            dt.Columns.Add("TaskSum");
            dt.Columns.Add("DoneSum", Type.GetType("System.Decimal"));
            dt.Columns.Add("HandSum");
            dt.Columns.Add("InstallSum");

            decimal sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
            if (ds.Tables[0].Rows.Count > 0)
            {
                while (dr.Read())
                {
                    dv1.RowFilter = dv2.RowFilter = dv3.RowFilter = dv4.RowFilter = "oiFiID='" + dr["fiID"].ToString() + "'";
                    DataTable dt1 = dv1.ToTable(), dt2 = dv2.ToTable(), dt3 = dv3.ToTable(), dt4 = dv4.ToTable();
                    dt.Rows.Add(dr["fiID"].ToString(), dt1.Rows.Count > 0 ? dt1.Rows[0]["oiSum"].ToString() : "0", dt2.Rows.Count > 0 ? dt2.Rows[0]["oiSum"].ToString() : "0", dt3.Rows.Count > 0 ? dt3.Rows[0]["oiSum"].ToString() : "0", dt4.Rows.Count > 0 ? dt4.Rows[0]["oiSum"].ToString() : "0");
                    sum1 += Convert.ToDecimal(dt1.Rows.Count > 0 ? dt1.Rows[0]["oiSum"].ToString() : "0");
                    sum2 += Convert.ToDecimal(dt2.Rows.Count > 0 ? dt2.Rows[0]["oiSum"].ToString() : "0");
                    sum3 += Convert.ToDecimal(dt3.Rows.Count > 0 ? dt3.Rows[0]["oiSum"].ToString() : "0");
                    sum4 += Convert.ToDecimal(dt4.Rows.Count > 0 ? dt4.Rows[0]["oiSum"].ToString() : "0");
                }
            }

            DataView dv = dt.DefaultView;
            dv.Sort = "DoneSum desc";
            dt = dv.ToTable();
            DataTable dt_chart = dt.Clone();
            if (dt.Rows.Count > 5)
            {
                for (int i = 0; i < 5; i++)
                {
                    dt_chart.Rows.Add(dt.Rows[i][0], dt.Rows[i][1], dt.Rows[i][2], dt.Rows[i][3], dt.Rows[i][4]);
                }
            }
            else
            {
                dt_chart = dt.Copy();
            }

            //设置图表的数据源
            Chart1.DataSource = dt_chart;

            //设置图表Y轴对应项
            Chart1.Series[0].YValueMembers = "TaskSum";
            Chart1.Series[1].YValueMembers = "DoneSum";
            Chart1.Series[2].YValueMembers = "HandSum";
            Chart1.Series[3].YValueMembers = "InstallSum";

            //设置图表X轴对应项
            Chart1.Series[0].XValueMember = "oiFiID";

            //绑定数据
            Chart1.DataBind();

            dt.Rows.Add("总计", sum1, sum2, sum3, sum4);
            GV.DataSource = dt;
            GV.DataBind();
        }

        /// <summary>响应GridView事件：创建数据行时
        /// 	<remark>2010-12-08 BY 河北大学信管团队</remark>
        /// </summary>
        protected void GV_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //为数据行增加鼠标移动到行特性
                e.Row.Attributes.Add("onmouseover", "c=style.backgroundColor;this.style.backgroundColor='" + IMIS_ERP.BLL.Assistant.getColor1 + "'");
                //为数据行增加鼠标离开行特性
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
    }
}